package cn.zbmao.web.util.mail;

import cn.zbmao.web.bean.User;
import cn.zbmao.web.config.TenderConfig;
import cn.zbmao.web.log.LoggerFactory;
import cn.zbmao.web.services.QualificationServices;
import cn.zbmao.web.services.UserServices;
import cn.zbmao.web.util.DateUtil;
import cn.zbmao.web.util.DegistUtil;
import cn.zbmao.web.util.dao.DaoFactory;
import com.jfinal.plugin.activerecord.Record;
import org.apache.commons.mail.EmailException;

import java.net.MalformedURLException;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class DailyMail {
private static  String HOST="http://42.51.7.86:8080";
	
private static SendMail sendMail = new SendMail();


static{
	String url = TenderConfig.objectUrl;
	if (url!=null) {
		HOST=url;
	}
}

	//发送邮件
	public static void doUserDailyMail(){
		List<User> userList = DaoFactory.getUserDao().getPayUsers();
		for (User user : userList) {
			List<Record> list = getNewsList(user);
			if (list.size()>0) {
				sendNewsMail(user, list);
			}
		}
	}

    public  static void sendNewsMail(User user,List<Record> listRecord){
		
		String mail = user.get("user_email");

		String md5 = DegistUtil.md5(Long.toString(new Date().getTime()));

		//自动登陆加密验证码
		//数据库user_login_auth_mail_md5 存储
		//user_login_auth_mail_time 存储当前时间
		user.set("user_login_auth_mail_md5", md5).set("user_login_auth_mail_time", DateUtil.getNowToDate()).update();

        //时间搓
        int timelong = DateUtil.getNowToInt()-60*60*24;

		//内容
		StringBuffer html = new StringBuffer();
		html.append("<html><div><h2>招标系统最新招标信息预览</h2>");
		
		html.append("<hr>");

		for (Record record : listRecord) {
			html.append("<ul style='list-style-type: none; padding: 0; margin: 20px 0 0 0;'>");
            html.append("<li style='margin: 2px 0;'>");
            html.append("入库日期：<span style='color:#008000'>");
            html.append(record.getStr("tlong"));
            html.append("</span>");
            html.append("</li>");
			html.append("<li style='margin: 2px 0;'>");
			html.append("投标结束日期：<span style='color:#008000'>");
			html.append(record.getStr("endTime"));
			html.append("</span>");
			html.append("</li>");
			html.append("<li style='margin: 2px 0;'>");

            //日期标红
            String NEW = record.get("new");
            if (NEW!=null&&NEW.equals("1")){
                html.append("[<span style='color: #dd4b39;'>新</span>]");
            }
//            点击标题时，跳到对应的详情页面
            html.append("<a href='"+HOST+"/news/detailsPage?id="+record.getInt("id")+"' target='_blank'>");
            html.append(record.getStr("title"));
            html.append("</a>");
			html.append("</li>");
			html.append("<li style='margin: 2px 0;'>");
			html.append("<span  style='color:#666; font-size: 9pt;'>");

            String num = record.getStr("num")==null?"":record.getStr("num");
            html.append("招标编号："+num);

            html.append("</span>");
			html.append("</li>");

            html.append("<li style='margin: 2px 0;'>");
            html.append("<span  style='color:#666; font-size: 9pt;'>");

            String qualification = QualificationServices.getQualificationForShow(record.getStr("qualification"));
            html.append("资质信息："+qualification);
            html.append("</span>");
            html.append("</li>");
            html.append("</ul>");
		}
		
		
		//包含url
		html.append("<p>点击登陆系统：");
        html.append(HOST);
		html.append("</p>");
		
		
		html.append("<p>如果无法点击上面的链接，您可以复制该地址，并粘帖在浏览器的地址栏中访问。</p><div></html>");
		
		
		//邮件主题
		String subject = "招标系统最新招标信息预览";
		
		//被动加载
		if (sendMail==null) {
			sendMail=new SendMail();
		}
		try {
			sendMail.sendHtmlEmail(mail, html.toString(), subject);
		} catch (MalformedURLException e) {
			LoggerFactory.initEmailLogger().error("MalformedURLException: "+e);
		} catch (EmailException e) {
			e.printStackTrace();
			LoggerFactory.initEmailLogger().error("EmailException: "+e);
		}
	}

    /**
     *
     * @param user 用户
     * @return 发送的数据
     * 当招标信息为过期，并且数据库入库时间小于当前日期减一天时间则添加‘新’
     */
	public static List<Record> getNewsList(User user){
		Map<String, List<Record>> map = UserServices.getUserInfo(user.getInt("id"));
        List<Record> list =  DaoFactory.getNewsDao().userNewsDailyMail(DateUtil.getTodayToInt()-1, map);
        for (Record record : list){
            record.set("new","1");
        }
        list.addAll(DaoFactory.getNewsDao().userDailyMail(DateUtil.getTodayToInt()-1, map));
		return list;
	}
}
